package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.UserController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.UserManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.ResponseJsonObject;
import com.hanoi.wiki.model.json.UserJson;

@Controller
@RequestMapping(value = "/usr")
public class UserControllerImpl extends BaseControllerImpl implements UserController {

	@Autowired
	private UserManager userManager;

	public @ResponseBody ResponseJsonObject getUser(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		UserJson json = null;
		try {
			json = userManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}	
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject saveUser(@RequestBody UserJson user) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		int id = 0;
		try {
			id = userManager.save(user);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		user.setId(id);
		objects.add(user);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeUser(@PathVariable int id){
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		UserJson json = null;
		
		try {
			json = userManager.get(id);
			userManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getUserList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(userManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateUser(@RequestBody UserJson user){
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			userManager.update(user);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(user);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	@RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
	public @ResponseBody
	ResponseJsonObject getUserByName(@PathVariable String name) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		UserJson json = null;
		try {
			json = userManager.getUserByName(name);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}	
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

}
